package com.shang.controller;

import com.shang.pojo.Orders;
import com.shang.pojo.Users;
import com.shang.pojo.vo.UsersVO;
import com.shang.service.center.MyOrdersService;
import com.shang.utils.RedisOperator;
import com.shang.utils.ShangJSONResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.UUID;

@Controller
public class BaseController {

    @Autowired
    private RedisOperator redisOperator;

    public static final Integer COMMON_PAGE_SIZE = 10;

    public static final Integer COMMENT_PAGE_SIZE = 10;
    public static final Integer PAGE_SIZE = 20;

    public static final String REDIS_USER_TOKEN = "redis_user_token";

    public static final String FOODIE_SHOPCART = "shopcart";

    String payReturnUrl = "http://localhost:8088/orders/notifyMerchantOrderPaid";

    String paymentUrl = "http://payment.t.mukewang.com/foodie-payment/payment/createMerchantOrder";		// produce

    public static String IMAGE_USER_FACE_LOCATION = File.separator + "workspaces" +
                                                        File.separator + "images" +
                                                        File.separator + "foodie" +
                                                        File.separator + "faces";


    @Autowired
    public MyOrdersService myOrdersService;

    /**
     * 用于验证用户和订单是否有关联关系，避免非法用户调用
     * @return
     */
    public ShangJSONResult checkUserOrder(String userId, String orderId) {
        Orders order = myOrdersService.queryMyOrder(userId, orderId);
        if (order == null) {
            return ShangJSONResult.errorMsg("订单不存在！");
        }
        return ShangJSONResult.ok(order);
    }


    public UsersVO conventUsersVO(Users user){
        //实现同步的Redis会话
        String uniqueToken = UUID.randomUUID().toString().trim();
        redisOperator.set(REDIS_USER_TOKEN + ":" + user.getId(),uniqueToken);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(user,usersVO);
        usersVO.setUserUniqueToken(uniqueToken);

        return usersVO;
    }
}
